<?php
if (!defined('IN_DISCUZ')){
	exit('Access Denied');
}

global $_G;
$row = $_G['product'];

//////////////////////////////////////////////KIEM TRA DIEU KIEN GIAN HANG//////////////////////////////////////////
if (!$uData)
	showmessage('ban_chua_dang_ky_gian_hang', 'shop.php?mod=store&ac=register');

if (!$_G['uid']){
	showmessage('postperm_login_nopermission', NULL, array(), array(
			'login'=>1
	));
}

if ($_G['adminid'] != 1){
	if ($uData['verificationed'] != 1)
		showmessage('you_not_complete_profile_to_post', 'shop.php?mod=store&ac=profile&do=update');
}

// Kiem tra categories da dang ky chua
if ($_G['adminid'] != 1){
	if ($uData['sid'] == 0)
		showmessage('ban_chua_dang_ky_dang_muc_ban_hang', 'shop.php?mod=store&ac=profile&do=update');
}

/*
 * Check Product id
 * */
if (!$row || $row['hide'] || (!$row['available'] && $_G['uid'] != $row['uid'] && $_G['adminid'] != 1)){
	showmessage('product_is_not_available', NULL, array(), array(
			'login'=>1
	));
}

if ($_G['uid'] != $row['uid'] && $_G['adminid'] != 1){
	showmessage('you_not_have_permission_to_edit_this_products', NULL, array(), array(
			'login'=>1
	));
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$submited = submitcheck('submit', 1, $seccodecheck);

if (!$submited){
	$product['pname'] = $row['pname'];
	$gp['pprice'] = number_format(round($row['pprice']), 0, '', '.');
	$product['punit'] = $row['punit'];
	$product['quantity'] = $row['quantity'];
	$gp['delivery_price'] = number_format(round($row['delivery_price']), 0, '', '.');
	$product['state'] = $row['state'];

	$product['sales']['rate'] = round($row['sales']);
	if ($product['sales'] > 0){
		$gp['sales']['from'] = $row['b_sales'] ? date('n/d/Y', $row['b_sales']) : '';
		$gp['sales']['end'] = $row['e_sales'] ? date('n/d/Y', $row['e_sales']) : '';
	}

	$gp['tax']['used'] = intval($row['tax']['used']);
	if ($gp['tax']['used'] == 1){
		$gp['tax']['tax_vat'] = $row['tax']['tax_vat'];
		$gp['tax']['tax_import'] = $row['tax']['tax_import'];
		$gp['tax']['tax_other'] = $row['tax']['tax_other'];
	}

	$product['status'] = $row['status'];
	$product['location_province'] = $row['location_province'];
	$product['summary'] = $row['summary'];
	$product['img_cover'] = $row['img_cover'];

	$editor['value'] = $row['detail'];

	//Thuoc tinh san pham
	if ($sid == $row['cid']){
		for ($i = 0; $i < $_G['shop']['param_number']; $i++){
			$gp['pparam']['custom_' . $i] = unserialize($row['custom_' . $i]);
		}
	}
}else{
	// Categories ID
	$product['cid'] = isset($_G['gp_cid']) ? intval($_G['gp_cid']) : 0;

	$product['pname'] = isset($_G['gp_pname']) ? get_str_nohtml($_G['gp_pname']) : '';
	if (strlen($product['pname']) < 6 || strlen($product['pname']) > 80)
		$post_erro['pname'] = 1;

	$gp['pprice'] = isset($_G['gp_pprice']) ? get_str_nohtml($_G['gp_pprice']) : '';
	$product['pprice'] = str_replace('.', '', $gp['pprice']);
	$product['pprice'] = intval($product['pprice']);
	if ($product['pprice'] < 2000)
		$post_erro['pprice'] = 1;

	$product['quantity'] = isset($_G['gp_quantity']) ? intval($_G['gp_quantity']) : 0;
	if ($product['quantity'] < 1)
		$post_erro['quantity'] = 1;

	$gp['delivery_price'] = isset($_G['gp_delivery_price']) ? get_str_nohtml($_G['gp_delivery_price']) : '';
	$product['delivery_price'] = str_replace('.', '', $gp['delivery_price']);
	$product['delivery_price'] = intval($product['delivery_price']);
	if ($product['delivery_price'] < 0)
		$product['delivery_price'] = 0;

	$product['punit'] = isset($_G['gp_unit']) ? get_str_nohtml($_G['gp_unit']) : '';

	$product['state'] = isset($_G['gp_state']) ? intval($_G['gp_state']) : 100;
	if ($product['state'] == 0)
		$post_erro['state'] = 1;

	/*
	 * Discount
	 * */
	$product['sales']['rate'] = isset($_G['gp_sales']['rate']) ? intval($_G['gp_sales']['rate']) : 0;
	$product['sales'] = $product['sales']['rate'] <= 0 ? 0 : ($product['sales']['rate'] > 100 ? 100 : $product['sales']['rate']);

	if ($product['sales'] > 0){
		$gp['sales']['from'] = isset($_G['gp_sales']['from']) ? get_str_nohtml($_G['gp_sales']['from']) : '';
		if (empty($gp['sales']['from']))
			$post_erro['sales']['from'] = 1;

		$product['b_sales'] = strtotime($gp['sales']['from']);

		$gp['sales']['end'] = isset($_G['gp_sales']['end']) ? get_str_nohtml($_G['gp_sales']['end']) : '';
		$product['e_sales'] = strtotime($gp['sales']['end']);
	}

	/*
	 * Thue cua san pham
	 * */
	$gp['tax']['used'] = isset($_G['gp_tax']['used']) ? intval($_G['gp_tax']['used']) : 0;
	if ($gp['tax']['used'] == 1){
		$gp['tax']['tax_vat'] = isset($_G['gp_tax']['tax_vat']) ? intval($_G['gp_tax']['tax_vat']) : 0;
		if ($gp['tax']['tax_vat'] == 0){
			$gp['tax']['tax_vat'] = 10;
			$post_erro['tax']['tax_vat'] = 1;
		}

		$gp['tax']['tax_import'] = isset($_G['gp_tax']['tax_import']) ? intval($_G['gp_tax']['tax_import']) : 0;
		$gp['tax']['tax_other'] = isset($_G['gp_tax']['tax_other']) ? intval($_G['gp_tax']['tax_other']) : 0;
	}
	if (!empty($gp['tax'])){
		$product['tax'] = serialize($gp['tax']);
	}

	$product['status'] = isset($_G['gp_status']) ? intval($_G['gp_status']) : 1;
	$product['location_province'] = isset($_G['gp_location_province']) ? intval($_G['gp_location_province']) : 0;

	$product['summary'] = isset($_G['gp_summary']) ? get_str_nohtml($_G['gp_summary']) : '';
	if (strlen($product['summary']) < 20 || strlen($product['summary']) > 300)
		$post_erro['summary'] = 1;

	// Image
	$product['img_cover'] = isset($_G['gp_imgdefault']) ? get_str_nohtml($_G['gp_imgdefault']) : '';
	if (!$product['img_cover'])
		$post_erro['img_cover'] = 1;

	$gp['images'] = isset($_G['gp_images']) ? $_G['gp_images'] : '';
	$product['images'] = serialize($gp['images']);

	$gp['extra'] = isset($_G['gp_extra']) ? get_str_nohtml($_G['gp_extra']) : '';

	for ($i = 0; $i < $_G['shop']['param_number']; $i++){
		if (!empty($gp['extra']['custom_' . $i])){
			$product['custom_' . $i] = addslashes(serialize($gp['extra']['custom_' . $i]));
		}
	}

	$editor['value'] = $product['detail'] = isset($_G['gp_detail']) ? censor(trim($_G['gp_detail'])) : '';

	if (empty($post_erro) && ($_G['adminid'] == 1 || $row['uid'] == $_G['uid'])){
		$pid = update_product($product, 'pid = ' . $row['pid']);

		// move image upload to /attach_dir/
		if (count($gp['images']) > 0){
			$imglistattach = array();

			foreach ($gp['images'] as $mid){
				if ($attach = DB::fetch(DB::query("SELECT * FROM " . DB::table("forum_attachment_unused") . " WHERE uid = " . $row['uid'] . " AND  aid = " . $mid))){
					$imglistattach[$attach['aid']] = $attach;
				}
			}

			move_attach($imglistattach, $row['pid'], 'insert', true);
			DB::update("shop_products", array(
					'available'=>TRUE
			), array(
					'pid'=>$row['pid']
			));
		}

		// Add feed
		if ($row['uid'] == $_G['uid']){
			require libfile('function/post');

			$pname = locdau($product['pname']);
			$feed = array(
					'icon'=>'',
					'title_template'=>'',
					'title_data'=>array(),
					'body_template'=>'',
					'body_data'=>array(),
					'title_data'=>array(),
					'images'=>array()
			);
			$feed['icon'] = 'product';
			$feed['title_template'] = 'feed_edit_product_title';
			$feed['body_template'] = 'feed_product_message';
			$feed['body_data'] = array(
					'subject'=>'<a href="shop.php?mod=view&name=' . $pname . '&pid=' . $row['pid'] . '">' . $product['pname'] . '</a>',
					'message'=>$product['summary'] . lang('shop/template', 'feed_gia_ban') . FormatNumber($product['pprice'])
			);
			$feed['images'] = array(
					'http://' . $_SERVER['HTTP_HOST'] . '/' . getattachURL($imglistattach[$product['img_cover']], 'shop/')
			);
			$feed['image_links'] = array(
					"shop.php?mod=view&name=" . $pname . "&pid=" . $row['pid']
			);
			$feed['title_data']['hash_data'] = "product{$row['pid']}";
			$feed['id'] = $pname;
			$feed['idtype'] = 'product';

			// Post feed to wall
			postfeed($feed);
		}
		/*
		 * Send notice to all friends
		 * */
		$listFriend = myfriend($_G['uid']);
		$listFriend = array_unique($listFriend);
		$update_array = array(
				'actor'=>$_G['username'],
				'url'=>'shop.php?mod=view&name=' . $pname . '&pid=' . $row['pid'],
				'pname'=>$product['pname']
		);
		if ($_G['adminid'] == 1 && $row['uid'] != $_G['uid']){
			notification_add($row['uid'], 'editproduct', 'edit_your_product', $update_array, 1);
		}else{
			foreach ($listFriend as $key=>$touid){
				notification_add($touid, 'editproduct', 'update_product', $update_array, 1);
			}
		}

		dsetcookie('shop', '');
		showmessage('post_update_product_is_success', 'shop.php?mod=view&pid=' . $row['pid']);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
include template('shop/store/edit_product');